package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sfmi.component.chnchannel.chncallcenter.client.FreeInsuranceClient;
import sfmi.component.chnchannel.chncallcenter.dto.common.Name;
import sfmi.component.chnchannel.chncallcenter.dto.enums.ConnectMode;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIPolicyholder;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIQuotation;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.key.FIQuotationKey;

public class saveQuotation extends HttpServlet {
	private static final Log log=LogFactory.getLog(saveQuotation.class);
	/**
	 * Constructor of the object.
	 */
	public saveQuotation() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String returnxml="0";
		String Address="";
		try{
			String Sex=request.getParameter("Sex");
			log.debug("Sex:"+Sex);
			String BirthDate=request.getParameter("BirthDate");
			log.debug("BirthDate:"+BirthDate);
			String CarOwnerYn=request.getParameter("CarOwnerYn");
			log.debug("CarOwnerYn:"+CarOwnerYn);
			Address=request.getParameter("Address");
			Address=URLDecoder.decode(Address,"UTF-8");
			log.debug("Address:"+Address);
			String MainLocationCode=request.getParameter("MainLocationCode");
			log.debug("MainLocationCode:"+MainLocationCode);
			String Email=request.getParameter("Email");
			log.debug("Email:"+Email);
			String PhoneNo=request.getParameter("PhoneNo");
			log.debug("PhoneNo:"+PhoneNo);
			String No=request.getParameter("No");
			log.debug("No:"+No);
			String NoTypeCode=request.getParameter("NoTypeCode");
			log.debug("NoTypeCode:"+NoTypeCode);
			String name1=request.getParameter("name1");
			name1=URLDecoder.decode(name1,"UTF-8");
			log.debug("name1:"+name1);
			String CouponNo=request.getParameter("CouponNo");
			log.debug("CouponNo:"+CouponNo);
			String IP=Util.getIp(request);
			log.debug("IP:"+IP);
			
			String noTypeName=request.getParameter("noTypeName");
			noTypeName=URLDecoder.decode(noTypeName,"UTF-8");
			log.debug("noTypeName:"+noTypeName);
			String mainLocationName=request.getParameter("mainLocationName");
			mainLocationName=URLDecoder.decode(mainLocationName,"UTF-8");
			log.debug("mainLocationName:"+mainLocationName);
			String sexName=request.getParameter("sexName");
			sexName=URLDecoder.decode(sexName,"UTF-8");
			log.debug("sexName:"+sexName);
			FIQuotationKey fiQuotationKey = new FIQuotationKey();
			fiQuotationKey.setIpAddress(IP);
			fiQuotationKey.setCouponNo(CouponNo);
			FIPolicyholder policyholder = new FIPolicyholder();
			Name name = new Name(); 
			name.setName(name1);
			policyholder.setName(name);
			policyholder.setNoTypeCode(NoTypeCode);
			policyholder.setNoTypeName(noTypeName);
			policyholder.setNo(No);
			policyholder.setPhoneNo(PhoneNo);
			policyholder.setEmail(Email);
			policyholder.setMainLocationCode(MainLocationCode);
			policyholder.setMainLocationName(mainLocationName);
			policyholder.setAddress(Address);
			policyholder.setCarOwnerYn(CarOwnerYn);
			policyholder.setAge(36);
			policyholder.setBirthDate(BirthDate);
			policyholder.setSexCode(Sex);
			policyholder.setSexName(sexName);
			fiQuotationKey.setPolicyholder(policyholder);
			FreeInsuranceClient freeInsuranceClient = new FreeInsuranceClient(ConnectMode.RealServer);
			FIQuotation f=freeInsuranceClient.saveQuotation(fiQuotationKey);
			returnxml=f.getQuotationNo();
		}catch(Exception e){
			log.error(Util.getERRO(e));
			returnxml="0";
		}
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=utf-8;");
	    PrintWriter out = response.getWriter();
	    log.debug("xml:"+returnxml);
	    out.print(returnxml);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
